package com.woorigil.social.domain.deal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.CouponSqlMapDaoSupport;

@Repository
public class DealDao extends CouponSqlMapDaoSupport<Deal, Integer> {
	public static final String NAMESPACE = "Deal";

	@Override
	public String getNamespace() {
		return NAMESPACE;
	}

	public Deal selectByMetaId(ProviderCode providerCode, String metaId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("providerCode", providerCode.name());
		params.put("metaId", metaId);
		return (Deal)getSqlMapClientTemplate().queryForObject(getStatementName(NAMESPACE, "selectByMetaId"), params);
	}
	
	public int selectMinId() {
		Object result = getSqlMapClientTemplate().queryForObject(getStatementName(NAMESPACE, "selectMinId"));
		return (result == null)? 0 : (Integer)result;
	}
	
	public int selectMaxId() {
		Object result = getSqlMapClientTemplate().queryForObject(getStatementName(NAMESPACE, "selectMaxId"));
		return (result == null)? 0 : (Integer)result;
	}

	@SuppressWarnings("unchecked")
	public List<Deal> search(DealCondition condition) {
		return getSqlMapClientTemplate().queryForList(getStatementName(NAMESPACE, "search"), condition);
	}

	public int updateStatus(ProviderCode providerCode, ManageStatus manageStatus) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("providerCode", providerCode.name());
		params.put("manageStatus", manageStatus.name());
		return getSqlMapClientTemplate().update(getStatementName(NAMESPACE, "updateStatus"), params);
	}

	public int updateByMetaId(Deal deal) {
		return getSqlMapClientTemplate().update(getStatementName(NAMESPACE, "updateByMetaId"), deal);
	}

	public int deleteByManageStatus(ProviderCode providerCode, ManageStatus manageStatus) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("providerCode", providerCode.name());
		params.put("manageStatus", manageStatus.name());
		return getSqlMapClientTemplate().update(getStatementName(NAMESPACE, "deleteByManageStatus"), params);
	}

	public int selectTotalCount(DealCondition condition) {
		Object result = getSqlMapClientTemplate().queryForObject(getStatementName(NAMESPACE, "selectTotalCount"), condition);
		return (result == null)? 0 : (Integer)result;
	}
}